package com.csj.DesignPattern.strategy;

public class SaleMan {

    private Strategy strategy;

    public SaleMan(Strategy strategy) {
        this.strategy = strategy;
    }

    //！！！！！方法的具体实现由用户传入的子类来实现
    public void sale() {
        strategy.show();
    }

    public Strategy getStrategy() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
}

/*
    public static void main(String[] args) {
        Integer[] data = {12, 3, 5, 17, 9};
        //Arrays对应SaleMan,Comparator接口对应Strategy接口
        //匿名内部类就是对应的StrategyA、StrategyB、StrategyC，他实现了Comparator接口
        Arrays.sort(data, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        System.out.println(Arrays.toString(data));
    }


    //本案例中是通过构造器或set来传入Strategy的子类
    //SaleMan saleMan = new SaleMan(new StrategyA());
    //或saleMan.setStrategy(new StrategyB());


    //而下面这个案例中，是通过参数直接传入,本质一样
    public static <T> void sort(T[] a, Comparator<? super T> c) {
        TimSort.sort(a, 0, a.length, c, null, 0, 0);
    }

    static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
                         T[] work, int workBase, int workLen) {
        int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
    }

    private static <T> int countRunAndMakeAscending(T[] a, int lo, int hi,
                                                    Comparator<? super T> c) {

        //调用了c里面的conpare方法
        //同public void sale() {
        //        strategy.show();
        //    }


        if (c.compare(a[runHi++], a[lo]) < 0) { // Descending
            while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
                runHi++;
            reverseRange(a, lo, runHi);
        } else {                              // Ascending
            while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0)
                runHi++;
        }
    }

 */
